package edu.gd.dao;

import edu.gd.entity.User;
import edu.gd.utils.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private static User getUser(ResultSet resultSet) throws SQLException {
        String user = resultSet.getString("user");
        String password = resultSet.getString("password");
        String sex=resultSet.getString("sex");
        String question=resultSet.getString("question");
        String answer=resultSet.getString("answer");
        String introduce=resultSet.getString("introduce");

        User u1=new User(user,password,sex,question,answer,introduce);
        return u1;
    }
    public static List<User> getUsers() {
        List<User> users = new ArrayList<>();
        Connection connection = DBUtils.getConnection();
        String sql = "select * from ttt";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareCall(sql);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                users.add(getUser(resultSet));
            }
            DBUtils.close(resultSet, statement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return users;
    }
    public static User getUserByID(String id){
        User u1=null;
        Connection connection = DBUtils.getConnection();
        String sql = "select * from ttt where user=?";
        PreparedStatement statement = null;
        try {
            statement = connection.prepareCall(sql);
            statement.setString(1,id);
            ResultSet resultSet = statement.executeQuery();
            if(resultSet.next()) {
                u1 = getUser(resultSet);
            }
            DBUtils.close(resultSet, statement, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return u1;
    }
    public static int delUserByID(String id){
        Connection connection=DBUtils.getConnection();
        String sql="delete from ttt where user= ?";
        int rows=0;
         try {
            PreparedStatement statement =connection.prepareCall(sql);
            statement.setString(1,id);
            rows=statement.executeUpdate();
            DBUtils.close(null,statement,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
         return rows;
    }
    public static  int updateUserByUser(User u1){
        Connection connection=DBUtils.getConnection();
        String sql="update ttt set sex=?,question=?,answer=?,introduce=? where user=?";
        int rows=0;
        try {
            PreparedStatement statement=connection.prepareCall(sql);
            statement.setString(1,u1.getSex());
            statement.setString(2,u1.getQuestion());
            statement.setString(3,u1.getAnswer());
            statement.setString(4,u1.getIntroduce());
            statement.setString(5,u1.getUsername());
            rows=statement.executeUpdate();
            DBUtils.close(null,statement,connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rows;
    }
}
